define(["angularAMD", "moment", "sortablejs"], function (angularAMD, moment, sortablejs) {
    angularAMD.service('IIoT0108_Service',
        ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$timeout', '$calculater',
            function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $timeout, $calculater) {
                this.init = function (options, dialog_feedback) {
                    var from_equipment_content = {};                            //來源設備查詢條件
                    from_equipment_content.equipment_no = options.equipment_no; //來源設備編號
                    from_equipment_content.work_date = options.work_date;       //來源設備派日期
                    from_equipment_content.request_type = "2";                  //查詢狀態 2 = 查設備派工 1 = 查設備清單
                    //取得來源設備
                    load_equipment_info(from_equipment_content, function (from_equipment_info) {
                        var equipment_dispatch_change_info = from_equipment_info.equipment_dispatch_change_info;
                        var available_time = from_equipment_info.available_time;
                        if (equipment_dispatch_change_info && available_time &&
                            equipment_dispatch_change_info.length > 0 && available_time.length > 0) {
                            var equipment_list_content = {};
                            equipment_list_content.request_type = "1";
                            equipment_list_content.area_no = equipment_dispatch_change_info[0].area_no;
                            //取得所有目的設備清單
                            load_equipment_info(equipment_list_content, function (list_info) {
                                var list_equipment_dispatch_change_info = $filter('filter')(list_info.equipment_dispatch_change_info, { equipment_no: "!" + options.equipment_no });
                                if (list_equipment_dispatch_change_info && list_equipment_dispatch_change_info.length > 0) {
                                    var to_equipment_content = {};
                                    to_equipment_content.equipment_no = list_equipment_dispatch_change_info[0].equipment_no;
                                    to_equipment_content.work_date = options.work_date;
                                    to_equipment_content.request_type = "2"
                                    //取得目的設備
                                    load_equipment_info(to_equipment_content, function (to_equipment_info) {
                                        var to_equipment_dispatch_change_info = to_equipment_info.equipment_dispatch_change_info;
                                        var to_available_time = to_equipment_info.available_time;
                                        var diff_op_list = [];
                                        for (var i in equipment_dispatch_change_info) {
                                            var has_count = $filter('filter')(diff_op_list, equipment_dispatch_change_info[i].op_no);
                                            if (has_count && has_count.length == 0)
                                                diff_op_list.push(equipment_dispatch_change_info[i].op_no);
                                        }
                                        var all_equipment_data = {};
                                        all_equipment_data.area_no = equipment_dispatch_change_info[0].area_no;
                                        all_equipment_data.op_no = diff_op_list.join("/");
                                        all_equipment_data.work_date = options.work_date;
                                        //form panel
                                        all_equipment_data.from_equipment_no = from_equipment_content.equipment_no;
                                        all_equipment_data.from_equipment_info = equipment_dispatch_change_info;
                                        all_equipment_data.from_available_time = from_equipment_info.available_time;
                                        //to panel
                                        all_equipment_data.to_equipment_no = to_equipment_content.equipment_no;
                                        all_equipment_data.to_equipment_info = to_equipment_dispatch_change_info;
                                        all_equipment_data.to_available_time = to_available_time;

                                        all_equipment_data.equipment_list_info = list_equipment_dispatch_change_info;
                                        //初始化 勾選 與 數量調整
                                        all_equipment_data.from_equipment_all_select = false;
                                        for (var i in all_equipment_data.from_equipment_info) {
                                            all_equipment_data.from_equipment_info[i].is_select = false;
                                            all_equipment_data.from_equipment_info[i].adjust_qty = (+all_equipment_data.from_equipment_info[i].qty);
                                            all_equipment_data.from_equipment_info[i].adjust_need_red = false;
                                        }
                                        all_equipment_data.from_equipment_info = run_time_counter(all_equipment_data.from_equipment_info, all_equipment_data.from_available_time);

                                        all_equipment_data.to_equipment_all_select = false;
                                        for (var i in all_equipment_data.to_equipment_info) {
                                            all_equipment_data.to_equipment_info[i].is_select = false;
                                            all_equipment_data.to_equipment_info[i].adjust_qty = (+all_equipment_data.to_equipment_info[i].qty);
                                            all_equipment_data.to_equipment_info[i].adjust_need_red = false;
                                        }
                                        all_equipment_data.to_equipment_info = run_time_counter(all_equipment_data.to_equipment_info, all_equipment_data.to_available_time);
                                        openDialog(all_equipment_data, dialog_feedback);
                                    });
                                }
                                else {
                                    //throw error no equipment list info
                                    $rootScope.showAlert($filter('translate')('IIoT0108.alert_note.not_find_equipment_list'));
                                }
                            });
                        }
                        else {
                            //throw error no from equipment info
                            $rootScope.showAlert($filter('translate')('IIoT0108.alert_note.not_find_from_equipment_info'));
                        }
                    });


                };
                //從後台取得 來源設備 目的設備 設備清單資料
                var load_equipment_info = function (content, callback) {
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_IIoT.equipment_dispatch_change_info_get',
                        content: content,
                        success: function (data) {
                            callback(data);
                        }
                    });
                }
                //派工 來源設備 目的設備 調整後結果
                var chage_equipment_dispath_process = function (content, callback) {
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_IIoT.equipment_dispatch_change_process',
                        content: content,
                        success: function (data) {
                            callback(data);
                        }
                    });
                }
                //開窗
                var openDialog = function (all_equipment_data, dialog_feedback) {
                    $mdDialog.dialog(
                        'module/moduleIIoT/program/IIoT01/IIoT0108/IIoT0108.html',
                        function (dialog) {
                            return {
                                css_url: 'module/moduleIIoT/program/IIoT01/IIoT0108/IIoT0108.css',
                                is_confirm: false,
                                all_equipment_data: all_equipment_data,
                                init: function () {
                                    //註冊拖甩排序功能
                                    dialog.from_panel_drag_sign_in();   //來源設備
                                    dialog.to_panel_drag_sign_in();     //目的設備
                                },
                                //撤銷
                                back_dispatch_action: function () {
                                    var from_back_eq_list = $filter('filter')(dialog.all_equipment_data.from_equipment_info, function (item) {
                                        if (item.adjust_qty > 0 && item.is_select)
                                            return item;
                                    });
                                    var to_back_eq_list = $filter('filter')(dialog.all_equipment_data.to_equipment_info, function (item) {
                                        if (item.adjust_qty > 0 && item.is_select)
                                            return item;
                                    });
                                    if (from_back_eq_list.length <= 0 && to_back_eq_list.length <= 0) {
                                        return $rootScope.showAlert($filter('translate')('IIoT0108.alert_note.not_back_need_select_lot'));
                                    }
                                    var new_from_temp_list = [];
                                    var new_to_temp_list = [];
                                    if (from_back_eq_list.length > 0) {
                                        for (var i in dialog.all_equipment_data.from_equipment_info) {
                                            var item = angular.copy(dialog.all_equipment_data.from_equipment_info[i]);
                                            item.is_select = false;
                                            item.adjust_need_red = false;
                                            var same_query = {
                                                lot_no: item.lot_no,
                                                op_no: item.op_no
                                            };
                                            var has_same_item = $filter('filter')(from_back_eq_list, same_query);
                                            if (has_same_item.length > 0) {
                                                var has_temp = $filter('filter')(new_from_temp_list, same_query);
                                                if (has_temp.length <= 0) {
                                                    var sum_qty = 0;
                                                    for (var j in has_same_item) {
                                                        sum_qty += (+ has_same_item[j].adjust_qty);
                                                    }
                                                    if (item.qty > sum_qty) {
                                                        item.qty -= sum_qty;
                                                        item.adjust_qty = item.qty;
                                                        item.seq_no = new_from_temp_list.length + 1;
                                                        new_from_temp_list.push(item);
                                                    }
                                                }
                                            }
                                            else {
                                                item.seq_no = new_from_temp_list.length + 1;
                                                new_from_temp_list.push(item);
                                            }
                                        }
                                        dialog.all_equipment_data.from_equipment_info = run_time_counter(new_from_temp_list, dialog.all_equipment_data.from_available_time);
                                    }
                                    if (to_back_eq_list.length > 0) {
                                        for (var i in dialog.all_equipment_data.to_equipment_info) {
                                            var item = angular.copy(dialog.all_equipment_data.to_equipment_info[i]);
                                            item.is_select = false;
                                            item.adjust_need_red = false;
                                            var same_query = {
                                                lot_no: item.lot_no,
                                                op_no: item.op_no
                                            };
                                            var has_same_item = $filter('filter')(to_back_eq_list, same_query);
                                            if (has_same_item.length > 0) {
                                                var has_temp = $filter('filter')(new_to_temp_list, same_query);
                                                if (has_temp.length <= 0) {
                                                    var sum_qty = 0;
                                                    for (var j in has_same_item) {
                                                        sum_qty += (+ has_same_item[j].adjust_qty);
                                                    }
                                                    if (item.qty > sum_qty) {
                                                        item.qty -= sum_qty;
                                                        item.adjust_qty = item.qty;
                                                        item.seq_no = new_to_temp_list.length + 1;
                                                        new_to_temp_list.push(item);
                                                    }
                                                }
                                            }
                                            else {
                                                item.seq_no = new_to_temp_list.length + 1;
                                                new_to_temp_list.push(item);
                                            }
                                        }
                                        dialog.all_equipment_data.to_equipment_info = run_time_counter(new_to_temp_list, dialog.all_equipment_data.to_available_time);
                                    }
                                },
                                //來源 與 目的 設備改派
                                open_transform_mode: function (trans_mode) {
                                    //trans_mode = 1 = 派工至目的設備 = go to to_equipment_info
                                    //trans_mode = 2 = 派工至來源設備 = go to from_equipment_info
                                    var need_move_lot_list = [];    //需移動生產批
                                    var has_change_lot_list = [];    //目的生產批
                                    var qty_has_zero = false;
                                    if (trans_mode == 1) {
                                        //派工至目的設備
                                        need_move_lot_list = angular.copy($filter('filter')(dialog.all_equipment_data.from_equipment_info, { is_select: true }));
                                        has_change_lot_list = angular.copy(dialog.all_equipment_data.to_equipment_info);
                                    }
                                    else {
                                        //派工至來源設備
                                        need_move_lot_list = angular.copy($filter('filter')(dialog.all_equipment_data.to_equipment_info, { is_select: true }));
                                        has_change_lot_list = angular.copy(dialog.all_equipment_data.from_equipment_info);
                                    }
                                    if (need_move_lot_list.length <= 0)
                                        // throw error
                                        return $rootScope.showAlert($filter('translate')('IIoT0108.alert_note.not_find_dispatch_info'));
                                    need_move_lot_list = $filter('orderBy')(need_move_lot_list, 'est_end_time');
                                    //console.log(need_move_lot_list);
                                    for (var i in need_move_lot_list) {
                                        if (need_move_lot_list[i].adjust_qty == 0) {
                                            qty_has_zero = true;
                                            break;
                                        }
                                        else {
                                            var product_no = need_move_lot_list[i].product_no;
                                            var op_no = need_move_lot_list[i].op_no;
                                            var area_no = need_move_lot_list[i].area_no;
                                            var has_same_time = $filter("filter")(has_change_lot_list, { area_no: area_no, product_no: product_no, op_no: op_no });
                                            need_move_lot_list[i].qty = (+need_move_lot_list[i].adjust_qty);
                                            if (has_same_time.length > 0) {
                                                need_move_lot_list[i].fix_eqp_time = (+has_same_time[0].fix_eqp_time);
                                                need_move_lot_list[i].var_eqp_time = (+has_same_time[0].var_eqp_time);
                                                need_move_lot_list[i].count_eqp_unit_qty = (+has_same_time[0].count_eqp_unit_qty);
                                            }
                                        }
                                    }
                                    if (qty_has_zero)
                                        return $rootScope.showAlert($filter('translate')('IIoT0108.alert_note.not_dispatch_need_qty'));

                                    var title = '';
                                    if (trans_mode == 1)
                                        title = $filter('translate')('IIoT0108.to_action_title');
                                    else
                                        title = $filter('translate')('IIoT0108.from_action_title');;
                                    var transform_list = [
                                        { label: $filter('translate')('IIoT0108.move_by_first'), code: 1 },
                                        { label: $filter('translate')('IIoT0108.move_by_last'), code: 2 },
                                        { label: $filter('translate')('IIoT0108.move_by_time'), code: 3 },
                                        { label: $filter('translate')('IIoT0108.move_by_canel'), code: 4 }
                                    ];
                                    $rootScope.showSelect({
                                        title: title,
                                        label: 'label',
                                        code: 'code',
                                        list: transform_list,
                                        confirm: function (item, select_dialog) {
                                            switch (item.code) {
                                                case 1://派工至第一筆 模式
                                                    var new_temp_lot_list = [];
                                                    has_change_lot_list = angular.copy(need_move_lot_list.concat(has_change_lot_list));
                                                    for (var i in has_change_lot_list) {
                                                        var same_query = {
                                                            lot_no: has_change_lot_list[i].lot_no,
                                                            op_no: has_change_lot_list[i].op_no
                                                        };
                                                        var has_same_item = $filter('filter')(has_change_lot_list, same_query);
                                                        if (has_same_item.length > 1) {
                                                            var has_temp = $filter('filter')(new_temp_lot_list, same_query);
                                                            if (has_temp == 0) {
                                                                var sum_qty = 0;
                                                                for (var j in has_same_item) {
                                                                    sum_qty += (+ has_same_item[j].qty);
                                                                }
                                                                has_change_lot_list[i].qty = sum_qty;
                                                                has_change_lot_list[i].adjust_qty = sum_qty;
                                                                has_change_lot_list[i].is_select = false;
                                                                has_change_lot_list[i].adjust_need_red = false;
                                                                has_change_lot_list[i].seq_no = (+i) + 1;
                                                                new_temp_lot_list.push(has_change_lot_list[i]);
                                                            }

                                                        }
                                                        else {
                                                            has_change_lot_list[i].is_select = false;
                                                            has_change_lot_list[i].adjust_need_red = false;
                                                            has_change_lot_list[i].seq_no = (+i) + 1;
                                                            new_temp_lot_list.push(has_change_lot_list[i]);
                                                        }
                                                    }
                                                    has_change_lot_list = new_temp_lot_list.slice();
                                                    break;
                                                case 2://依推估完工時間派工 模式
                                                    var new_temp_lot_list = [];
                                                    has_change_lot_list = angular.copy(has_change_lot_list.concat(need_move_lot_list));
                                                    for (var i = has_change_lot_list.length - 1; i >= 0; i--) {
                                                        var same_query = {
                                                            lot_no: has_change_lot_list[i].lot_no,
                                                            op_no: has_change_lot_list[i].op_no
                                                        };
                                                        var has_same_item = $filter('filter')(has_change_lot_list, same_query);
                                                        if (has_same_item.length > 1) {
                                                            var has_temp = $filter('filter')(new_temp_lot_list, same_query);
                                                            if (has_temp == 0) {
                                                                var sum_qty = 0;
                                                                for (var j in has_same_item) {
                                                                    sum_qty += (+ has_same_item[j].qty);
                                                                }
                                                                has_change_lot_list[i].qty = (+sum_qty);
                                                                has_change_lot_list[i].adjust_qty = (+sum_qty);
                                                                has_change_lot_list[i].is_select = false;
                                                                has_change_lot_list[i].adjust_need_red = false;
                                                                has_change_lot_list[i].seq_no = (+i) + 1;
                                                                new_temp_lot_list.push(has_change_lot_list[i]);
                                                            }

                                                        }
                                                        else {
                                                            has_change_lot_list[i].is_select = false;
                                                            has_change_lot_list[i].seq_no = (+i) + 1;
                                                            new_temp_lot_list.push(has_change_lot_list[i]);
                                                        }
                                                    }
                                                    has_change_lot_list = new_temp_lot_list.reverse();
                                                    break;
                                                case 3://派工至最後一筆 模式
                                                    var new_temp_lot_list = [];
                                                    has_change_lot_list = angular.copy(has_change_lot_list.concat(need_move_lot_list));
                                                    has_change_lot_list = $filter('orderBy')(has_change_lot_list, ['est_end_time', 'lot_no', 'op_no']);
                                                    for (var i in has_change_lot_list) {
                                                        var same_query = {
                                                            lot_no: has_change_lot_list[i].lot_no,
                                                            op_no: has_change_lot_list[i].op_no
                                                        };
                                                        var has_same_item = $filter('filter')(has_change_lot_list, same_query);
                                                        if (has_same_item.length > 0) {
                                                            var has_temp = $filter('filter')(new_temp_lot_list, same_query);
                                                            if (has_temp == 0) {
                                                                var sum_qty = 0;
                                                                for (var j in has_same_item) {
                                                                    sum_qty += (+ has_same_item[j].adjust_qty);
                                                                }
                                                                has_change_lot_list[i].qty = (+sum_qty);
                                                                has_change_lot_list[i].adjust_qty = (+sum_qty);
                                                                has_change_lot_list[i].is_select = false;
                                                                has_change_lot_list[i].adjust_need_red = false;
                                                                has_change_lot_list[i].seq_no = (+i) + 1;
                                                                new_temp_lot_list.push(has_change_lot_list[i]);
                                                            }
                                                        }
                                                        else {
                                                            has_change_lot_list[i].is_select = false;
                                                            has_change_lot_list[i].seq_no = (+i) + 1;
                                                            new_temp_lot_list.push(has_change_lot_list);
                                                        }
                                                    }
                                                    has_change_lot_list = new_temp_lot_list.slice();
                                                    break;
                                            }
                                            //調整來源設備資料 排除需移動生產批
                                            if (trans_mode == 1) {
                                                var temp_list = [];
                                                for (var i in dialog.all_equipment_data.from_equipment_info) {
                                                    var qty = dialog.all_equipment_data.from_equipment_info[i].qty;
                                                    var same_query = {
                                                        lot_no: dialog.all_equipment_data.from_equipment_info[i].lot_no,
                                                        op_no: dialog.all_equipment_data.from_equipment_info[i].op_no
                                                    };
                                                    dialog.all_equipment_data.from_equipment_info[i].is_select = false;
                                                    dialog.all_equipment_data.from_equipment_info[i].adjust_need_red = false;
                                                    var need_remove_item = $filter('filter')(need_move_lot_list, same_query);
                                                    if (need_remove_item.length > 0) {
                                                        if ((+qty) - (+need_remove_item[0].adjust_qty) > 0) {
                                                            dialog.all_equipment_data.from_equipment_info[i].qty -= (+need_remove_item[0].adjust_qty);
                                                            dialog.all_equipment_data.from_equipment_info[i].adjust_qty = (+dialog.all_equipment_data.from_equipment_info[i].qty);
                                                            temp_list.push(dialog.all_equipment_data.from_equipment_info[i]);
                                                        }
                                                    }
                                                    else {
                                                        temp_list.push(dialog.all_equipment_data.from_equipment_info[i]);
                                                    }

                                                }
                                                dialog.all_equipment_data.from_equipment_info = angular.copy(temp_list)
                                                dialog.all_equipment_data.to_equipment_info = angular.copy(has_change_lot_list);
                                            }
                                            else {
                                                var temp_list = [];
                                                for (var i in dialog.all_equipment_data.to_equipment_info) {
                                                    var qty = dialog.all_equipment_data.to_equipment_info[i].qty;
                                                    var same_query = {
                                                        lot_no: dialog.all_equipment_data.to_equipment_info[i].lot_no,
                                                        op_no: dialog.all_equipment_data.to_equipment_info[i].op_no
                                                    };
                                                    dialog.all_equipment_data.to_equipment_info[i].is_select = false;
                                                    dialog.all_equipment_data.to_equipment_info[i].adjust_need_red = false;
                                                    var need_remove_item = $filter('filter')(need_move_lot_list, same_query);
                                                    if (need_remove_item.length > 0) {
                                                        if ((+qty) - (+need_remove_item[0].adjust_qty) > 0) {
                                                            dialog.all_equipment_data.to_equipment_info[i].qty -= (+need_remove_item[0].adjust_qty);
                                                            dialog.all_equipment_data.to_equipment_info[i].adjust_qty = (+dialog.all_equipment_data.to_equipment_info[i].qty);
                                                            temp_list.push(dialog.all_equipment_data.to_equipment_info[i]);
                                                        }
                                                    }
                                                    else {
                                                        temp_list.push(dialog.all_equipment_data.to_equipment_info[i]);
                                                    }
                                                }
                                                dialog.all_equipment_data.to_equipment_info = angular.copy(temp_list)
                                                dialog.all_equipment_data.from_equipment_info = angular.copy(has_change_lot_list);
                                            }
                                            //重新計算推估時間(來源 與 目的)
                                            //console.log(dialog.all_equipment_data.to_equipment_info);
                                            //console.log(dialog.all_equipment_data.from_equipment_info);
                                            dialog.all_equipment_data.to_equipment_info = run_time_counter(dialog.all_equipment_data.to_equipment_info, dialog.all_equipment_data.to_available_time);
                                            dialog.all_equipment_data.from_equipment_info = run_time_counter(dialog.all_equipment_data.from_equipment_info, dialog.all_equipment_data.from_available_time);
                                            select_dialog.hide();
                                        }
                                    });
                                },
                                qty_check: function (event, item) {
                                    var target = event.currentTarget;
                                    angular.element(target).addClass('is-focus');
                                    $calculater.open(function (num, num_dialog) {
                                        if (+item.qty < num || num == 0)
                                            return $rootScope.showAlert($filter('translate')('IIoT0108.alert_note.qty_check_not_pass'));
                                        item.adjust_qty = num;
                                        angular.element(target).removeClass('is-focus');
                                        num_dialog.hide();
                                    },
                                        function (num_dialog) {
                                            angular.element(target).removeClass('is-focus');
                                            num_dialog.hide();
                                        },
                                        {
                                            defaultNum: item.adjust_qty,
                                            decimal: true
                                        });
                                },
                                from_panel_select_all_action: function () {
                                    if (all_equipment_data.from_equipment_all_select) {
                                        for (var i in all_equipment_data.from_equipment_info)
                                            all_equipment_data.from_equipment_info[i].is_select = false;
                                        all_equipment_data.from_equipment_all_select = false;
                                    }
                                    else {
                                        for (var i in all_equipment_data.from_equipment_info)
                                            all_equipment_data.from_equipment_info[i].is_select = true;
                                        all_equipment_data.from_equipment_all_select = true;
                                    }
                                },
                                to_panel_select_all_action: function () {
                                    if (all_equipment_data.to_equipment_all_select) {
                                        for (var i in all_equipment_data.to_equipment_info)
                                            all_equipment_data.to_equipment_info[i].is_select = false;
                                        all_equipment_data.to_equipment_all_select = false;
                                    }
                                    else {
                                        for (var i in all_equipment_data.to_equipment_info)
                                            all_equipment_data.to_equipment_info[i].is_select = true;
                                        all_equipment_data.to_equipment_all_select = true;
                                    }
                                },
                                from_panel_select_list_action: function (from_item) {
                                    if (from_item.is_select)
                                        from_item.is_select = false;
                                    else
                                        from_item.is_select = true;
                                    var has_select_count = $filter("filter")(all_equipment_data.from_equipment_info, { is_select: true }).length;
                                    if (has_select_count == all_equipment_data.from_equipment_info.length)
                                        all_equipment_data.from_equipment_all_select = true;
                                    else
                                        all_equipment_data.from_equipment_all_select = false;
                                },
                                to_panel_select_list_action: function (to_item) {
                                    if (to_item.is_select)
                                        to_item.is_select = false;
                                    else
                                        to_item.is_select = true;
                                    var has_select_count = $filter("filter")(all_equipment_data.to_equipment_info, { is_select: true }).length;
                                    if (has_select_count == all_equipment_data.to_equipment_info.length)
                                        all_equipment_data.to_equipment_all_select = true;
                                    else
                                        all_equipment_data.to_equipment_all_select = false;
                                },
                                from_panel_drag_sign_in: function () {
                                    $timeout(function () {
                                        sortablList = sortablejs.create(document.getElementById('IIoT_from_panel'), {
                                            // ghostClass: 'ghost',     //給予拖曳物件 add class
                                            // filter: '.undragable',   //過濾不能拖曳物件 by class   
                                            handle: '.sortable-from-btn',
                                            onEnd: function (evt) {
                                                var oldIndex = evt.oldIndex;
                                                var newIndex = evt.newIndex;
                                                var new_temp_array = dialog.all_equipment_data.from_equipment_info.slice();
                                                var oldItem = new_temp_array[oldIndex];
                                                var newItem = new_temp_array[newIndex];
                                                if (oldItem && newItem) {
                                                    // console.log("change_old:"+newIndex+":"+oldItem.lot_no);
                                                    // console.log("change_new:"+oldIndex+":"+newItem.lot_no);
                                                    new_temp_array.splice(oldIndex, 1, newItem);
                                                    new_temp_array.splice(newIndex, 1, oldItem);
                                                    dialog.all_equipment_data.from_equipment_info = new_temp_array;
                                                    dialog.all_equipment_data.from_equipment_all_select = false;
                                                    for (var i in dialog.all_equipment_data.from_equipment_info) {
                                                        dialog.all_equipment_data.from_equipment_info[i].is_select = false;
                                                        dialog.all_equipment_data.from_equipment_info[i].adjust_qty = (+all_equipment_data.from_equipment_info[i].qty);
                                                        dialog.all_equipment_data.from_equipment_info[i].adjust_need_red = false;
                                                    }
                                                    dialog.all_equipment_data.from_equipment_info =
                                                        run_time_counter(dialog.all_equipment_data.from_equipment_info, dialog.all_equipment_data.from_available_time);
                                                    $rootScope.$apply();
                                                }
                                            }
                                        });
                                    });
                                },
                                to_panel_drag_sign_in: function () {
                                    $timeout(function () {
                                        sortablList = sortablejs.create(document.getElementById('IIoT_to_panel'), {
                                            // ghostClass: 'ghost',     //給予拖曳物件 add class
                                            // filter: '.undragable',   //過濾不能拖曳物件 by class   
                                            handle: '.sortable-to-btn',
                                            onEnd: function (evt) {
                                                var oldIndex = evt.oldIndex;
                                                var newIndex = evt.newIndex;
                                                var new_temp_array = dialog.all_equipment_data.to_equipment_info.slice();
                                                var oldItem = new_temp_array[oldIndex];
                                                var newItem = new_temp_array[newIndex];
                                                if (oldItem && newItem) {
                                                    // console.log("change_old:"+newIndex+":"+oldItem.lot_no);
                                                    // console.log("change_new:"+oldIndex+":"+newItem.lot_no);
                                                    new_temp_array.splice(oldIndex, 1, newItem);
                                                    new_temp_array.splice(newIndex, 1, oldItem);
                                                    dialog.all_equipment_data.to_equipment_info = new_temp_array;
                                                    $rootScope.$apply();
                                                    dialog.all_equipment_data.to_equipment_all_select = false;
                                                    for (var i in dialog.all_equipment_data.to_equipment_info) {
                                                        dialog.all_equipment_data.to_equipment_info[i].is_select = false;
                                                        dialog.all_equipment_data.to_equipment_info[i].adjust_qty = (+all_equipment_data.from_equipment_info[i].qty);
                                                        dialog.all_equipment_data.to_equipment_info[i].adjust_need_red = false;
                                                    }
                                                    dialog.all_equipment_data.to_equipment_info =
                                                        run_time_counter(dialog.all_equipment_data.to_equipment_info, dialog.all_equipment_data.to_available_time);
                                                    $rootScope.$apply();
                                                }
                                            }
                                        });
                                    });
                                },
                                open_equipment_list: function () {
                                    $rootScope.showQueryList({
                                        key: 'equipment_no',
                                        header: [
                                            { label: $filter('translate')('common.queryList.machine_c'), code: 'equipment_no' },
                                            { label: $filter('translate')('common.queryList.machine_n'), code: 'equipment_name' }
                                        ],
                                        query: function (conditions, feedback) {
                                            feedback(dialog.all_equipment_data.equipment_list_info);
                                        },
                                        selectCode: dialog.all_equipment_data.to_equipment_no,
                                        confirm: function (selectItem, equipment_list_dialog) {
                                            dialog.all_equipment_data.to_equipment_no = selectItem.equipment_no;
                                            equipment_list_dialog.hide();
                                            var to_equipment_content = {};
                                            to_equipment_content.equipment_no = dialog.all_equipment_data.to_equipment_no;
                                            to_equipment_content.work_date = dialog.all_equipment_data.work_date;
                                            to_equipment_content.request_type = "2"
                                            load_equipment_info(to_equipment_content, function (to_equipment_info) {
                                                dialog.all_equipment_data.to_equipment_info = to_equipment_info.equipment_dispatch_change_info;
                                                dialog.all_equipment_data.to_available_time = to_equipment_info.available_time;
                                                dialog.all_equipment_data.to_equipment_all_select = false;
                                                for (var i in dialog.all_equipment_data.to_equipment_info) {
                                                    dialog.all_equipment_data.to_equipment_info[i].is_select = false;
                                                    dialog.all_equipment_data.to_equipment_info[i].adjust_qty = (+dialog.all_equipment_data.to_equipment_info[i].qty);
                                                    dialog.all_equipment_data.to_equipment_info[i].adjust_need_red = false;
                                                }
                                                dialog.all_equipment_data.to_equipment_info = run_time_counter(dialog.all_equipment_data.to_equipment_info, dialog.all_equipment_data.to_available_time);
                                            });
                                        }
                                    });
                                },
                                date_format: function (date_str) {
                                    var str = moment(date_str).format("HH:mm:ss");
                                    var range_day = moment(date_str).diff(dialog.all_equipment_data.work_date, 'days')
                                    if (range_day >= 1)
                                        str = "*" + str;
                                    return str;
                                },
                                //送出
                                confirm: function () {
                                    var content = {};
                                    content.work_date = dialog.all_equipment_data.work_date;
                                    content.target_equipment_no = dialog.all_equipment_data.to_equipment_no;
                                    content.source_equipment_no = dialog.all_equipment_data.from_equipment_no;
                                    content.source_dispatch_list = [];  //來源設備
                                    content.target_dispatch_list = [];  //目的設備
                                    //來源設備資料轉換
                                    for (var i in dialog.all_equipment_data.from_equipment_info) {
                                        var item = dialog.all_equipment_data.from_equipment_info[i];
                                        var obj = {};
                                        obj.equipment_no = dialog.all_equipment_data.from_equipment_no;
                                        obj.work_date = dialog.all_equipment_data.work_date;
                                        obj.seq = item.seq_no;
                                        obj.lot_no = item.lot_no;
                                        obj.op_no = item.op_no;
                                        obj.qty = item.qty;
                                        obj.std_disp_start_time = item.std_disp_start_time;
                                        obj.std_disp_end_time = item.std_disp_end_time;
                                        obj.disp_start_time = item.est_start_time;
                                        obj.disp_end_time = item.est_end_time;
                                        obj.disp_start_time_pre = item.schedule_start_time;
                                        obj.disp_end_time_pre = item.schedule_end_time;
                                        content.source_dispatch_list.push(obj);
                                    }
                                    //目的設備資料轉換
                                    for (var i in dialog.all_equipment_data.to_equipment_info) {
                                        var item = dialog.all_equipment_data.to_equipment_info[i];
                                        var obj = {};
                                        obj.equipment_no = dialog.all_equipment_data.to_equipment_no;
                                        obj.work_date = dialog.all_equipment_data.work_date;
                                        obj.seq = item.seq_no;
                                        obj.lot_no = item.lot_no;
                                        obj.op_no = item.op_no;
                                        obj.qty = item.qty;
                                        obj.std_disp_start_time = item.std_disp_start_time;
                                        obj.std_disp_end_time = item.std_disp_end_time;
                                        obj.disp_start_time = item.est_start_time;
                                        obj.disp_end_time = item.est_end_time;
                                        obj.disp_start_time_pre = item.schedule_start_time;
                                        obj.disp_end_time_pre = item.schedule_end_time;
                                        content.target_dispatch_list.push(obj);
                                    }
                                    chage_equipment_dispath_process(content, function (data) {
                                        $rootScope.showToast($filter('translate')('IIoT0108.success'));
                                        dialog.is_confirm = true;
                                        dialog.hide();
                                        dialog_feedback(dialog);
                                    });
                                },
                                back: function () {
                                    dialog.hide();
                                    dialog_feedback(dialog);
                                }
                            }
                        }

                    );
                }
                //推估時間計算
                var run_time_counter = function (equment_info, available_time) {
                    var time_index = 0;                                 //班別索引
                    var time_list = angular.copy(available_time);       //班別時間列表
                    var start_time = time_list[time_index].start_time;  //每輪生產批 開始時間
                    var end_time = time_list[time_index].end_time;      //每輪生產批 結束時間
                    var date_format = 'YYYY-MM-DD HH:mm:ss';             //日期格式
                    var eq_info = angular.copy(equment_info);          //生產批資訊
                    for (var i in eq_info) {
                        var item = eq_info[i];                          //每批生產批
                        console.log(item.qty + "*" + item.var_eqp_time + "/" + item.count_eqp_unit_qty + "+" + item.fix_eqp_time);
                        item.dispatch_rate = parseInt(item.qty * item.var_eqp_time / item.count_eqp_unit_qty + item.fix_eqp_time);
                        //先取第一個班別的時段差(開始 - 結束) 來判斷 每批生產批的產能是否足夠
                        if (time_list[time_index].available_capacity - (+item.dispatch_rate) >= 0) {
                            // 該生產批 產能足夠扣
                            eq_info[i].est_start_time = moment(start_time).format(date_format);
                            eq_info[i].est_end_time = moment(start_time).add(item.dispatch_rate, 'minutes').format(date_format);
                            start_time = moment(eq_info[i].est_end_time).format(date_format);
                            time_list[time_index].available_capacity -= (+item.dispatch_rate);
                        }
                        else {
                            // 該生產批 產能不足夠扣
                            var remain_range = time_list[time_index].available_capacity;    //上一班剩餘時段差(開始 - 結束)
                            var current_range = item.dispatch_rate - remain_range;          //此次需扣除的生產批產能 = 當前生產批產能 - 上一班剩餘時段差
                            //班別往後推一筆
                            if (time_index < time_list.length - 1)
                                time_index++;
                            //依照此次需扣除的生產批產能 找最近的班別索引
                            for (var j = time_index; j < time_list.length; j++) {
                                //判斷此次需扣除的生產批產能 能否扣除 班別時段差
                                if (current_range - time_list[j].available_capacity > 0) {
                                    //生產批產能夠扣 將產能減去 繼續往下找
                                    current_range -= time_list[j].available_capacity;
                                    //當該班別為最後一筆時 將班別索引指向 跳脫迴圈
                                    if (j == time_list.length - 1) {
                                        time_index = j;
                                        //結束時間 指向
                                        end_time = moment(time_list[j].end_time).add(current_range, 'minutes').format(date_format);
                                        break;
                                    }
                                }
                                else {
                                    //生產批產能不夠扣 將班別索引指向 跳脫迴圈
                                    time_index = j;
                                    //結束時間 指向
                                    if (moment(start_time).diff(time_list[j].start_time, 'minutes') < 0)
                                        end_time = moment(time_list[j].start_time).add(current_range, 'minutes').format(date_format);
                                    else
                                        end_time = moment(start_time).add(current_range, 'minutes').format(date_format);
                                    break;
                                }
                            }
                            var end_time_range = moment(end_time).diff(time_list[j].end_time, 'minutes');
                            if (end_time_range > 0) {
                                //console.log(end_time+":"+time_list[j].end_time);
                                end_time = moment(time_list[j].end_time).format(date_format);
                            }
                            eq_info[i].est_start_time = moment(start_time).format(date_format);
                            eq_info[i].est_end_time = moment(end_time).format(date_format);

                            if (end_time_range > 0) {
                                //console.log(end_time_range);
                                //console.log(" end_time_range:"+end_time_range);
                                //console.log(" current:"+current_range);
                                //console.log(" rate:"+item.dispatch_rate);
                                //console.log("fix_eqp_time:"+eq_info[i].fix_eqp_time+" count_eqp_unit_qty:"+eq_info[i].count_eqp_unit_qty+" var_time:"+eq_info[i].var_eqp_time);
                                //console.log("adjust_qty:"+adjust_qty);
                                //console.log(" org_time:"+org_end_time+" end_time:"+end_time+" range:"+moment(org_end_time).diff(end_time,'minutes'));
                                //var org_end_time = moment(end_time).add(end_time_range,'minutes').format(date_format)
                                //計算需撤回數量
                                var adjust_qty = Math.ceil((end_time_range) * eq_info[i].count_eqp_unit_qty / eq_info[i].var_eqp_time);
                                var run_red_indx = (+i);
                                if (adjust_qty > 0 && adjust_qty <= eq_info[i].adjust_qty)
                                    eq_info[i].adjust_qty = (+parseInt(adjust_qty));
                                for (var j = run_red_indx; j < eq_info.length; j++) {
                                    eq_info[j].adjust_need_red = true;
                                    // if(j == run_red_indx )
                                    //     eq_info[j].adjust_need_red = true;
                                    // else
                                    // {
                                    //     if(j != run_red_indx )
                                    //     {
                                    //         eq_info[j].adjust_need_red = true;
                                    //         //eq_info[j].adjust_qty = (+ eq_info[j].qty);
                                    //     }   
                                    // }
                                }
                            }
                            //將 這一輪的結束時間 塞入 開始時間 並將班別時段差 歸零 已繼續 run 下一次產能 
                            start_time = moment(end_time).format(date_format);
                            for (var j = 0; j <= time_index; j++)
                                time_list[j].available_capacity = 0;
                        }
                    }
                    return eq_info;
                }
            }]);
});